package ioperation;

import Book.Book;
import Book.BookShelf;

import java.util.Scanner;

public class DeleteOperation implements IOperation {
    public void work(BookShelf bookShelf) {
        int count = bookShelf.getNum();
        System.out.println("请输入你要删除的书名：");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        int pos=-1;
        int i=0;
        //查找到要删除的书的位置(下标)
        for (i = 0; i < count; i++) {
            Book book = bookShelf.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("已找到你想要删除的图书！");
                pos=i;
                break;
            }
            if(i==count){
                System.out.println("没有你要删除的图书！");
                return;
        }
            }
        //删除操作
        for (int j = pos; j < count-1;j++) {
            Book book=bookShelf.getBook(j+1);
            bookShelf.setBooks(j,book);

        }
        bookShelf.setNum(count-1);
        System.out.println("删除成功！");
    }
}
